package snake.controller;

import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;

import snake.entities.Food;
import snake.entities.Ground;
import snake.entities.Snake;
import snake.listener.SnakeListener;
import snake.util.Global;
import snake.view.GamePanel;

public class Controller extends KeyAdapter implements SnakeListener{
	
	private Snake snake;
	private Food food;
	private Ground ground;
	private GamePanel gamePanel;
	
	public Controller(Snake snake, Food food, Ground ground, GamePanel gamePanel) {
		super();
		this.snake = snake;
		this.food = food;
		this.ground = ground;
		this.gamePanel = gamePanel;
	}

	@Override
	public void keyPressed(KeyEvent e) {
	    switch(e.getKeyCode()){
			case KeyEvent.VK_UP:
				snake.changeDirection(Snake.UP);
				break;
			case KeyEvent.VK_DOWN:
				snake.changeDirection(Snake.DOWN);
				break;
			case KeyEvent.VK_LEFT:
				snake.changeDirection(Snake.LEFT);
				break;
			case KeyEvent.VK_RIGHT:
				snake.changeDirection(Snake.RIGHT);
				break;
		}	  
	}

	@Override
	public void snakeMoved(Snake snake) {	
		if(food.isSnakeEatFood(snake)){
			snake.eatFood();
			snake.move();
			newFood(getPoint());
		}
		else if(ground.isSnakeEatRock(snake)){			
			snake.die();
		}
		else if(snake.isEatBody()){
			snake.die();
		}
		gamePanel.display(snake, food, ground);		
	}
	public Point getPoint(){
		Random random = new Random();	
		int x,y;
		do{
			x = random.nextInt(Global.LENGTH);
			y = random.nextInt(Global.HEIGHT);			
		}while(ground.rocks[x][y]==1);		
		return new Point(x,y);
	}
	
	public void newFood(Point p){
		food.setLocation(p);
	}
	
	public void newGame(){
		snake.startThread();
		newFood(getPoint());
	}
}
